import DataProvider from "../db/DataProvider.js";
import { agent } from "../iService/Service.js"
export default function feature(router) {
    router.get("/feature", async(ctx, next) => {
        let URL = "/areas_v3/bound/geojson";
        let urlParams = ctx.request.url.split("?")
        let params = {
            hostname: 'geo.datav.aliyun.com',
            headers: { 'content-type': "application/json" },
            method: "GET",
            path: URL + '?' + ctx.querystring
        }
        ctx.body = await agent().request(params)
        await next()
    })


    router.get("/table", async(ctx, next) => {
        let provide = new DataProvider();
        let query = ctx.query;
        let page = query.page || 1,
            size = query.size || 10;
        let total = await provide.count('station', {})
        let data = await provide.findAndPaginate('station', {}, { st_code: -1 }, { page: page, size: page * size > total ? (total - size * (page - 1)) : size })
        if (data) {
            ctx.body = {
                flag: true,
                data: data,
                pagination: { size, page, total }
            }
        } else {
            ctx.body = {
                flag: false,
                data: [],
                pagination: { size, page, total: 0 }
            }
        }
        await next()
    })

    router.post('/axis', async(ctx, next) => {
        let provide = new DataProvider();
        let data = await provide.find('station', {}, { st_code: -1 }, { _id: 0, st_lat: true, st_long: true })
        if (data) {
            ctx.body = {
                flag: true,
                data: data.map(sts => ({ lat: sts.st_lat, lng: sts.st_long }))
            }
        } else {
            ctx.body = {
                flag: false,
                data: []
            }
        }
        await next()
    })

    return router.routes()
}